package com.xdcplus.web.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.xdcplus.mom.pojo.vo.SysBaseDataVO;
import com.xdcplus.web.common.pojo.entity.pm.PmShiftManage;
import com.xdcplus.web.common.pojo.entity.pm.PmShiftPlan;
import com.xdcplus.web.common.pojo.vo.PmShiftManageVO;
import com.xdcplus.web.mapper.CommonMapper;
import com.xdcplus.web.mapper.pm.PmShiftManageMapper;
import com.xdcplus.web.mapper.pm.PmShiftPlanMapper;
import com.xdcplus.web.service.CommonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@Service
public class CommonServiceImpl
        implements CommonService {

    final static String commonParams = "MES_REPORT_PARAMS";

    @Autowired
    protected PmShiftPlanMapper pmShiftPlanMapper;

    @Autowired
    protected PmShiftManageMapper pmShiftManageMapper;

    @Autowired
    protected CommonMapper commonMapper;

    @Override
    public SysBaseDataVO getSysBaseDataByIdAndExt(String id, String ext) {
        return commonMapper.getSysBaseDataByIdAndExt(commonParams,id,ext);
    }


    public PmShiftManageVO getPmShiftByDate(Date date) {
        if (date == null) {
            date = new Date();
        }
        PmShiftManageVO pmShiftManageVO=new PmShiftManageVO();
        String dateString = DateUtil.format(date, "yyyy-MM-dd");
        String timeString = DateUtil.format(date, "HH:mm:ss");

        /// 获取日期的排班计划
        List<PmShiftPlan> pmShiftPlans = pmShiftPlanMapper.queryPmShiftPlanByDate(dateString);
        if (CollectionUtil.isEmpty(pmShiftPlans)) {
            return pmShiftManageVO;
        }
        /// 按计划确定班次信息
        List<Long> shiftTypes = pmShiftPlans.stream().map(PmShiftPlan::getShiftType).filter(Objects::nonNull).distinct().collect(Collectors.toList());
        List<PmShiftManage> pmShiftManage = pmShiftManageMapper.queryPmShiftManageByTime(timeString, shiftTypes,null);

        /// 为查到班制，有可能跨天了
        if (CollectionUtil.isEmpty(pmShiftManage)) {
            dateString= date.toInstant().atOffset(ZoneOffset.ofHours(8)).toLocalDateTime()
                    .minusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            List<PmShiftPlan> pmShiftPlanExt = pmShiftPlanMapper.queryPmShiftPlanByDate(
                    dateString
            );
            if (CollectionUtil.isEmpty(pmShiftPlanExt)) {
                return pmShiftManageVO;
            }

            List<Long> shiftTypeExt = pmShiftPlanExt.stream().map(PmShiftPlan::getShiftType).filter(Objects::nonNull).distinct().collect(Collectors.toList());
            List<PmShiftManage> pmShiftManageExt = pmShiftManageMapper.queryPmShiftManageByTime(timeString, shiftTypeExt,1);
            if (CollectionUtil.isEmpty(pmShiftManageExt)) {
                return pmShiftManageVO;
            }

            BeanUtil.copyProperties(
                    pmShiftManageExt.get(0),pmShiftManageVO
            );

        }else {
            if (pmShiftManage.get(0).getDataAdjust()==1) {
                if (timeString.compareTo(pmShiftManage.get(0).getStartTime()) <= 0) {
                    dateString= date.toInstant().atOffset(ZoneOffset.ofHours(8)).toLocalDateTime()
                            .minusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                }
            }
            BeanUtil.copyProperties(
                    pmShiftManage.get(0),pmShiftManageVO
            );
        }
        pmShiftManageVO.setShiftDate(dateString);
        return pmShiftManageVO;
    }


}
